﻿#region  文件信息
// ---------------------------------------------------------------------------------------
// <copyright file="SubJsonBase.cs" company="">
// 作者：李帅伦
// </copyright>
// <summary>
// 类型简介：
// 类型详细：SubJsonBase.cs
// 创建时间：2015-03-06 19:18
// 修改时间：2015-03-06 18:31
// </summary>
// ---------------------------------------------------------------------------------------
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Jiekou.Com
{
    #region

    

    #endregion

    /// <summary>
    ///     提交json
    /// </summary>
    [JsonObject(MemberSerialization.OptIn)]
    public   class SubJson  : IJsonData
    {
        #region Constructors and Destructors

        public SubJson ()
        {

        }

        /// <summary>
        /// 提交json
        /// </summary>
        /// <param name="json">
        /// json字符串
        /// </param>
        public SubJson (string json)
        {
            this.IsValidate = true;
            try
            {
                var jObject = JObject.Parse(json);


                if (jObject["fn"] == null)
                {
                    this.SetMsg("Fn为空");
                }
                else
                {
                    this.Fn = jObject["fn"].ToString();
                }


                if (jObject["suid"] == null)
                {
                    this.SetMsg("Suid为空");
                }
                else
                {
                    this.Suid =  jObject["suid"].ToString() ;
                }

                if (jObject["check"] == null)
                {
                    this.SetMsg("Check为空");
                }
                else
                {
                    this.Check = jObject["check"].ToString();
                }

                if (jObject["data"] != null)
                {
                    this.Jsondata = jObject["data"].ToString();
                    this.Data = jObject["data"].ToObject<object>();
                }

                
            }
            catch
            {
                this.SetMsg("json格式错误");
            }
        }

        #endregion

        #region Public Properties
 

        /// <summary>
        ///     验证
        /// </summary>
        [JsonProperty("check")]
        public string Check { get; set; }


        /// <summary>
        ///     数据集
        /// </summary>
        [JsonProperty("data")]
        public object Data { get; set; }

        /// <summary>
        ///     功能码
        /// </summary>
        [JsonProperty("fn")]
        public string Fn { get; set; }

        /// <summary>
        ///     是否通过验证
        /// </summary>
        protected bool IsValidate { get; set; }

        /// <summary>
        /// Gets or sets the jsondata.
        /// </summary>
        public string Jsondata { get; protected set; }

        /// <summary>
        /// The set data.
        /// </summary>
        /// <param name="jsondata">
        /// The jsondata.
        /// </param>
        /// <returns>
        /// The <see cref="object"/>.
        /// </returns>
        public object SetData(string jsondata)
        {
            return null;
        }

        /// <summary>
        ///     信息
        /// </summary>
        public string Msg { get; set; }

        /// <summary>
        ///     系统用户id
        /// </summary>
        [JsonProperty("suid")]
        public string Suid { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// 查看是否有fn
        /// </summary>
        /// <param name="fns">
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        private bool IsFns(IEnumerable<string> fns)
        {
            var any = fns.Any(a => a == this.Fn);
            if (!any)
            {
                this.SetMsg("接口类型不存在");
            }

            return any;
        }

        /// <summary>
        /// 查看密钥是否正确
        /// </summary>
        /// <param name="key">
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        private bool IsSuids(string key)
        {
            if (this.IsValidate)
            {
                var m = this.Check;
                this.Check = key;
                var settings = new JsonSerializerSettings();
                settings.NullValueHandling = NullValueHandling.Ignore;
                var serializeObject = JsonConvert.SerializeObject(this, settings);
                var md5 = this.Md5(serializeObject);
                var b = m == md5;
                if (!b)
                {
                    this.SetMsg("加密验证错误");
                }

                return b;
            }

            return false;
        }

        #endregion

        #region Methods

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="str">
        /// The str.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        protected string Md5(string str)
        {
            return WebCommon.Encryption.Md532(str);
        }

        /// <summary>
        /// 写入msg
        /// </summary>
        /// <param name="msg">
        /// 消息，返回false
        /// </param>
        protected void SetMsg(string msg)
        {
            if (this.IsValidate)
            {
                this.Msg = msg;
                this.IsValidate = false;
            }
        }

        /// <summary>
        /// Getjson
        /// </summary>
        /// <param name="fns">
        /// The fns.
        /// </param>
        /// <param name="md5">
        /// The md 5.
        /// </param>
        /// <returns>
        /// </returns>
        public bool Validate(string md5)
        {
            if (string.IsNullOrWhiteSpace(md5))
            {
                this.SetMsg("加密验证错误");
                return false;
            }

            if (this.IsValidate)
            {
                var isSuids = this.IsSuids(md5);
                if (!isSuids)
                {
                    return false;
                }

                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// GetReturnJson
        /// </summary>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public RetJson GetReturnJson()
        {
            return new RetJson(this);
            
        }

        #endregion

     
    }
}